/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cirnoworks.hpop.log;

import com.cirnoworks.http.utils.DummyOutputStream;
import com.cirnoworks.http.utils.HTTPUtils;
import com.cirnoworks.http.utils.HeaderRequest;
import com.cirnoworks.http.utils.HeaderResponse;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 *
 * @author yuxuanhuang
 */
public class LogAnalyzer implements Closeable {

    private HeaderRequest reqHeader = new HeaderRequest();
    private HeaderResponse resHeader = new HeaderResponse();
    private InputStream is;

    public LogAnalyzer(InputStream is) {
        try {
            this.is = is;
            HTTPUtils.readLine(is);
            HTTPUtils.readFirstLineRequest(is, reqHeader);
            HTTPUtils.readHeader(is, reqHeader);
            HTTPUtils.deliveryEntity(reqHeader, is, DummyOutputStream.getInstance(), DummyOutputStream.getInstance(), true);
            HTTPUtils.readLine(is);
            HTTPUtils.readHeaderResponse(is, resHeader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void outputContent(OutputStream os) {
        HTTPUtils.deliveryEntity(resHeader, is, DummyOutputStream.getInstance(), os, false);
    }

    public void close() throws IOException {
        if (is != null) {
            is.close();
        }
    }
}
